package cn.com.myapplication;

import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

/**
 * @Description:
 * @see:
 * @since:
 * @author:zhonggaoyong
 * @copyright
 * @Date:2023/2/19
 */
class RestaurantDisplayDialog extends Presentation {

    public RestaurantDisplayDialog(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.activity_display, null);
        setContentView(view);

        final TextView widthPxText = (TextView) findViewById(R.id.main_et_width_px);
        final TextView heightPxText = (TextView) findViewById(R.id.main_et_height_px);
        final TextView densityText = (TextView) findViewById(R.id.main_et_density);
        final TextView densityDpiText = (TextView) findViewById(R.id.main_et_density_dpi);
        final TextView widthDipText = (TextView) findViewById(R.id.main_et_width_dip);
        final TextView heightDipText = (TextView) findViewById(R.id.main_et_height_dip);
                int widthPx =getContext().getResources()
                        .getDisplayMetrics().widthPixels;
                // 屏幕高度(px)
                int heightPx =getContext().getResources()
                        .getDisplayMetrics().heightPixels;
                widthPxText.setText(widthPx + "");
                heightPxText.setText(heightPx + "");
                // 屏幕密度:指每平方英寸中的像素数,在DisplayMetrics类中，该密度值为dpi/160
                float density = getContext().getResources()
                        .getDisplayMetrics().density;
                // 屏幕密度(dpi):指每英寸中的像素数
                float densityDpi = getResources()
                        .getDisplayMetrics().densityDpi;
                densityText.setText(density + "");
                densityDpiText.setText(densityDpi + "");
                // 屏幕宽度(dip)
                int widthDip = pxToDip(getContext(), widthPx);
                // 屏幕高度(dip)
                int heightDip = pxToDip(getContext(), heightPx);
                widthDipText.setText(widthDip + "");
                heightDipText.setText(heightDip + "");
    }


    /**
     * px值向dip值转换
     *
     * @param context
     * @param pxValue
     * @return
     */
    private int pxToDip(Context context, float pxValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * dip值向px值转换
     *
     * @param context
     * @param dipValue
     * @return
     */
    public int dipToPx(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
